package cn.mdmm.controller.system;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.mdmm.common.SpringSecurityUtils;
import cn.mdmm.controller.BaseController;
import cn.mdmm.entity.response.ResponseBodyToOne;
import cn.mdmm.entity.system.MenuTree;
import cn.mdmm.entity.system.SysUser;
import cn.mdmm.service.system.SysMenuService;
import cn.mdmm.service.system.SysUserService;
import cn.modoumama.common.annotation.OpLog;
import cn.modoumama.common.utils.ConfigProperty;
@Controller
@RequestMapping(value = "/")
public class IndexController extends BaseController {
	@Autowired
	private SysMenuService sysMenuService;
	@Autowired
	private SysUserService sysUserService;
	
	@RequestMapping(value = "")
	public String index(){
		return "redirect:security/main";
	}
	
	@RequestMapping(value = "index/tree")
	@OpLog(logDesc="系统操作员获取菜单")
	@ResponseBody
	public List<MenuTree> index(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception{
		SysUser user = (SysUser)session.getAttribute("user");
		List<MenuTree> menuTrees = sysMenuService.listUserTree(user.getId(),false,false);
		return menuTrees;
	}
	@RequestMapping(value = "index/password")
	public String password(){
		return "password";
	}
	@RequestMapping(value = "index/updatePwd")
	@OpLog(logDesc="系统操作员修改密码")
	@ResponseBody
	public ResponseBodyToOne checkUserPwd(HttpServletRequest request){
		ResponseBodyToOne responseBody = new ResponseBodyToOne();
		SysUser sUser =SpringSecurityUtils.getCurrentUser();
		String userPwd = request.getParameter("userPwd");
		if(StringUtils.isBlank(userPwd)){	
			userPwd = ConfigProperty.getProperty("default.password");
		}
		String newPwd = request.getParameter("newPwd");
		String confirmPwd = request.getParameter("confirmPwd");
		sysUserService.updateUserPwd(sUser.getId(), userPwd, newPwd, confirmPwd);
		return responseBody;
	}
	@RequestMapping(value = "index/update")
	public String update(HttpServletRequest request){
		return "success";
	}
}
